import crypto from 'crypto'
import fs from 'fs'
import path from 'path';

const rootPath = process.env.BASE_PATH || __dirname

export const publicKey = crypto.createPublicKey(fs.readFileSync(path.resolve(rootPath, 'config/rsa/public.pem'), 'utf-8'))
export const privateKey = crypto.createPrivateKey({
    key: fs.readFileSync(path.resolve(rootPath, 'config/rsa/private.pem'), 'utf-8'),
    passphrase: 'viya.top'
})

export const publicEncrypt = (text: string) => {
    return crypto.publicEncrypt(publicKey, Buffer.from(text)).toString('base64')
}

export const privateDecrypt = (text: string) => {
    return crypto.privateDecrypt(privateKey, Buffer.from(text, 'base64')).toString('utf-8')
}


